package com.ae.shield.common.gui.container;

import com.ae.shield.common.gui.ShieldSlots;
import com.ae.shield.common.items.shieldFittings.makers.RubbishShield;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;

import javax.annotation.Nonnull;

public class RubbishContainer extends Container {
    private final ItemStack caller;

    public RubbishContainer(int id, PlayerInventory inventory, PacketBuffer buffer) {
        this(id, inventory, buffer.readItemStack());
    }

    public RubbishContainer(int id, PlayerInventory inventory, ItemStack stack) {
        super(ContainerList.RUBBISH_CONTAINER.get(), id);
        caller = stack;

        IInventory callerInv;
        if (!inventory.player.world.isRemote) {
            callerInv = RubbishShield.getInventory(caller);
        }else {
            callerInv = new Inventory(RubbishShield.SIZE);
        }

        //创建玩家背包
        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 9; column++) {
                addSlot(new Slot(inventory, column + row * 9 + 9, 8 + column * 18, 126 - (4 - row) * 18 - 10));
            }
        }
        //创建工具栏
        for (int col = 0; col < 9; ++col) {
            addSlot(new Slot(inventory, col, 8 + col * 18, 102));
        }

        for (int i = 0; i < RubbishShield.SIZE; i++) {
            addSlot(new ShieldSlots.FilterSlot(callerInv, i, 8 + 18 * i, 15));
        }
    }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity p_82846_1_, int p_82846_2_) {
        return ItemStack.EMPTY;
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        Slot slot = slotId < 0 ? null : getSlot(slotId);

        //不是过滤用格子按照默认方式处理
        if (!(slot instanceof ShieldSlots.FilterSlot)) {
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        ItemStack pickStack = player.inventory.getItemStack();
        ItemStack copyStack = pickStack.copy();
        copyStack.setCount(1);
        ItemStack slotStack = slot.getStack();

        slot.putStack(copyStack);

        return slotStack;
    }

    @Override
    public boolean canInteractWith(@Nonnull PlayerEntity playerIn) {
        return true;
    }
}
